<template>
    <c-list-template title="车辆上架管理" :operation="true">
        <template slot="search">

            <el-row :gutter="5">
                <el-col :span="8" class="flex-center">
                    <div class="mr-lg">创建时间</div>
                    <el-date-picker v-model="querys.timeExpiry" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-col>
                <el-col :span="6" class="flex-center">
                    <div class="mr-lg">车辆信息</div>
                    <el-input v-model="querys.productInfo" placeholder="请输入车辆信息" class="wh flex-1 mr-lg" />
                </el-col>
                <el-col :span="6" class="flex-center ">
                    <div class="mr-lg">状态：</div>
                    <el-select v-model="querys.status" placeholder="请选择">
                        <el-option v-for="(item,index) in productStatus" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="4" class="flex-x-end">
                    <el-button icon="el-icon-search" type="primary" @click="handleSelect">搜索</el-button>
                    <el-button icon="el-icon-delete" @click="handleReset">重置</el-button>
                </el-col>
            </el-row>
        </template>
        <template slot="operation">
            <div class="flex-y-center">
                <el-button type="primary" @click="batchApprove">批量审批</el-button>
            </div>
        </template>
        <template slot="list">
            <el-table :header-cell-style="{ background: '#F1F1F1', color: '#092132' }" :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
                <el-table-column label="序号" align="center" width="80" type="index" />
                <el-table-column label="商品标题" width="300" align="center" prop="productTitle" />
                <el-table-column label="车辆信息" width="300" align="center" prop="productInfo" />
                <el-table-column label="库存（辆）" align="center" prop="stockAmount" />
                <el-table-column label="状态" width="150" align="center">
                    <template slot-scope="scope">
                        {{scope.row.status == null ? "" : productStatus[scope.row.status].label }}
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        {{ scope.row.timeCreate | dateYMDHMSFormat }}
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center">
                    <template slot-scope="scope">
                        <el-link type="primary" v-if="scope.row.status == 0" @click="handleDetails(scope.row,false)">审核</el-link>
                        <el-link type="primary" v-if="scope.row.status == 2" @click="handleDelisting(scope.row)">下架</el-link>
                        <el-link type="primary" v-if="scope.row.status != 0" @click="handleDetails(scope.row,true)">详情</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </template>
        <template slot="pagination">
            <el-pagination background :current-page="querys.current" :page-sizes="[10, 20, 50, 100]" :page-size="querys.size" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </template>
        <c-approval-dialog ref="cApprovalDialog" />
    </c-list-template>
</template>
<script>
import { productStatus } from "@/utils/status.js";
import cApprovalDialog from './components/c-approval-dialog.vue';
export default {
    components: { cApprovalDialog },
    data () {
        return {
            total: 0,
            tableData: [],
            querys: {
                current: 1,
                size: 10,
                productInfo: "",
                status: ""
            },
            loading: false,
            productStatus: productStatus,
            checkList: []
        };
    },
    mounted () {
        this.tableList();
    },
    methods: {
        selectable: function (row, index) {
            if (row.status == 0) {
                return true;
            }
        },
        batchApprove () {
            if (this.checkList.length == 0) {
                this.$message.warning("请选择批量处理的车辆");
                return false;
            }
            this.$refs.cApprovalDialog.handleOpen(this.checkList)
        },
        handleSelectionChange (val) {
            this.checkList = val
        },
        handleDelisting (item) {
            this.$confirm("是否确认下架该车辆?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.$post({
                    url: "/product/listing/off?id=" + item.id,
                }).then(
                    () => {
                        this.$message.success("车辆已下架");
                        this.tableList();
                    }
                );
            })
        },
        handleDetails (item, isView) {
            this.$router.push({
                path: "/listingReview/details",
                query: {
                    id: item.id,
                    isView: isView
                },
            });
        },
        tableList () {
            this.loading = true;
            const params = JSON.parse(JSON.stringify(this.querys));
            const url = "/product/listing/page";
            this.$get({ url, params }).then((res) => {
                this.tableData = res.records;
                this.total = res.total * 1;
                this.loading = false;
            });
        },
        handleReset () {
            this.querys = {
                current: 1,
                size: 10,
            };
            this.tableList();
        },
        handleSelect () {
            this.querys.current = 1;
            this.tableList();
        },
        handleSizeChange (val) {
            this.querys.size = val;
            this.tableList();
        },
        handleCurrentChange (val) {
            this.querys.current = val;
            this.tableList();
        },
    },
};
</script>
